package cn.csg.iotplatform.module.system.service.permission;

import cn.csg.iotplatform.module.common.imports.ExcelImportException;
import cn.csg.iotplatform.module.common.imports.ExcelValidate;
import cn.csg.iotplatform.module.common.util.ExcelImportUtil;
import cn.csg.iotplatform.module.system.controller.admin.permission.vo.role.RoleImportExcelDto;
import cn.csg.iotplatform.module.system.dal.dataobject.permission.RoleDO;
import cn.csg.iotplatform.module.system.dal.mysql.permission.RoleMapper;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import jodd.util.StringUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class RoleImportValidate extends ExcelValidate {

    private final RoleMapper roleMapper;

    public RoleImportValidate() {
        this.roleMapper = SpringUtil.getBean(RoleMapper.class);
    }

    @Override
    protected String validateAndFormatColumn(Integer rowIndex, Integer colIndex, String columnName, String value, Map<String, String> row) throws ExcelValidateException {
        switch (columnName) {
            case "角色名称":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                if (StringUtil.isBlank(value)) {
                    break;
                }
                RoleDO role = roleMapper.selectByName(value);
                if (role != null) {
                    throw new ExcelValidateException("已存在");
                }
                break;
            case "是否公用":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                String[] isPublicArray = {"是", "否"};
                List<String> isPublicList = Arrays.asList(isPublicArray);
                if (!isPublicList.contains(value)) {
                    throw new ExcelValidateException("非枚举内选项");
                }
                break;
            case "管理范围":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                String[] dataScopeArray = {"本单位及下级单位", "仅本单位"};
                List<String> dataScopeList = Arrays.asList(dataScopeArray);
                if (!dataScopeList.contains(value)) {
                    throw new ExcelValidateException("非枚举内选项");
                }
                break;
            default:
                break;
        }
        return null;
    }

    @Override
    protected void validateHead(Map<Integer, String> heard) throws ExcelImportException {
        ExcelImportUtil.checkHeaderByEntity(RoleImportExcelDto.class, heard.values());
    }
}
